$(document).ready(function(){
        var pdown = 0;
        var pup = 0;
	/* The following code is executed once the DOM is loaded */
	
	$('.sponsorFlip').bind("click",function(){
		// $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):
		
		var elem = $(this);
                
		// data('flipped') is a flag we set when we flip the element:
		if(elem.attr("aberto") != 'sim' && $('.sponsorFlip[aberto]').length < 2 && elem.html() == ''){
                   if(elem.data('flipped'))
                    {
                            // If the element has already been flipped, use the revertFlip method
                            // defined by the plug-in to revert to the default state automatically:
                            elem.revertFlip();
                            // Unsetting the flag:
                            elem.data('flipped',false)
                            elem.html('');
                    }
                    else
                    {
                            elem.attr('aberto','sim');
                            // Using the flip method defined by the plugin:
                            $.post( ROOT+'pt/acao/buscamemoria/'+$(this).attr('identificador'), function( data ){},"JSON")
                                .done(function( data ) {
                                    elem.flip({
                                        direction:'lr',
                                        speed: 800,
                                        onBefore: function(){
                                                // Insert the contents of the .sponsorData div (hidden from view with display:none)
                                                // into the clicked .sponsorFlip div before the flipping animation starts:
                                                elem.html('<img src="'+ROOT+'assets/uploads/nt_memorias/MIN'+data.IMAGEM+'" alt="'+data.NOME+'" title="'+data.NOME+'">');                    
                                        }
                                    });		

                                elem.data('flipped',true);
                            })
                                .always(function( data ) {
                                    
                                     setTimeout(function(){
                                        if($('.sponsorFlip[aberto]').length == 2){
                                            if($('.sponsorFlip[aberto]').eq(0).html() == $('.sponsorFlip[aberto]').eq(1).html()){
                                                pup++;
                                                $.post( ROOT+'pt/acao/salvaacerto/memoria/'+data.ID);
                                                getAlert("Acertou");
                                                $('.sponsorFlip').removeAttr('aberto');
                                                $('.acerto').html(pup);
                                            }else{
                                                    pdown++;
                                                    $('.sponsorFlip[aberto]').data('flipped',false);
                                                    $('.sponsorFlip[aberto]').revertFlip();
                                                    $('.sponsorFlip[aberto]').html('');
                                                    $('.sponsorFlip').removeAttr('aberto');
                                                    $('.erro').html(pdown);
                                            }
                                        }
                                    },3000);
                            });
                    }     
                }
	});
	
});